if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
      ready();
  }
})

function ready(){
  var user = {};
  var html = [];
  var uploadControl={};
  var thisPage = {};
  var id = getUrlParam("id");
  var orderAppraisePage = function(){
    this.init();
  };
  orderAppraisePage.prototype = {
    init:function(){
      this.initData();
      this.bindEvent();
    },
    initData:function(){
      user = getLocalStorage(STORAGE_MANAGER.user);
      if(!user){
        redirect("../account/login.html");
        return;
      }
      
      if(id){
        requestUrl = CONFIG_MANAGER.RequstUrl + "v1/orders/" + id;
      }else{
        $.toast("没有订单号");
        return;
      }
      $.ajax({
        type: "GET",
        url: requestUrl,
        async: true,
        headers: { "token": user.token },
        beforeSend: function () {
          $.showWaiting();
        }
      }).done(function (data) {
        thisPage.loadOrderDetail(data);
      }).always(function(data){
        $.closeWaiting();
      });
    },
    loadOrderDetail:function(data){
      var items = data.items;
      html = [];
      $.each(items, function(i, item) {
        html.push('<div name="goodsItem" tip="'+ item.sku_id +'" class="wlxqbg">');
        html.push('<div class="wlxqnrbg mbtopbottom">');
        html.push('<p class="shxxbt1  mbbottom"><span>'+ item.shop_name +'</span></p>');
        html.push('<div class="mbbottom with100 overflow">');
        html.push('<dl class="cpxqxztop cpxqxztop1 mtop0">');
        html.push('<dt><img src="'+ getImageUrl(item.logo, 63, 63) +'"></dt>');
        html.push('<dd class="wdgzwz wdgzwz-10">');
        html.push('<p class="floatr">');
        html.push('<span class="jinbi"></span>');
        html.push('<span class="money money3">￥'+ item.act_price +'</span>');
        html.push('</p>');
        html.push(item.sku_name);
        html.push('</dd>');
        html.push('<dd class="huisezi"><span class="x2">x'+ item.qty +'</span>'+ item.specs +'</dd>');
        html.push('</dl>');
        html.push('</div>');
        html.push('<div class="pjddbg">');
        html.push('<p class="pjddbt">');
        html.push('<b>商品评价</b>');
        html.push('<span name="grade">');
        html.push(thisPage.paintStar(0));
        html.push('</span>');
        html.push('</p>');
        html.push('<p class="pjddk">');
        html.push('<textarea name="content" cols="" rows="" placeholder="说写什么吧~" class="pjddwb"></textarea>');
        html.push('</p>');        
        html.push('<p class="fxxfxdtu" id="showAlbums" style="margin:10px 0px;">');
        html.push('<span name="selectUploadType" class="tianjiaan" style="position: relative;">');
        if(AGENT_CLIENT.WEB){
          html.push('<input name="addImgFile" class="a_upload" type="file" capture="camera" accept="image/png,image/gif,image/jpg,image/jpeg" style="height: 70px;">');
        }
        html.push('<img src="../../images/tjantu.png">');
        html.push('</span>');
        html.push('</p>');        
        html.push('</div>');
        html.push('</div>');
        html.push('</div>');        
      });
      
      html.push('<div class="dppjianbg mbtopbottom">');
      html.push('<p class="shxxbt1 mbbottom">');
      html.push('<span>店铺评价</span>');
      html.push('</p>');
      html.push('<div class="pjlxk">');
      html.push('<p class="pjddbt pjddbt1">');
      html.push('<b>&bull;商品包装</b>');
      html.push('<span name="grade_package">');
      html.push(thisPage.paintStar(0));
      html.push('</span>');
      html.push('</p>');
      html.push('<p class="pjddbt pjddbt1">');
      html.push('<b>&bull;送货速度</b>');
      html.push('<span name="grade_deliveriy">');
      html.push(thisPage.paintStar(0));
      html.push('</span>');
      html.push('</p>');
      html.push('<p class="pjddbt pjddbt1">');
      html.push('<b>&bull;配送服务</b>');
      html.push('<span name="grade_service">');
      html.push(thisPage.paintStar(0));
      html.push('</span>');
      html.push('</p>');
      html.push('</div>');
      html.push('</div>');
      html.push('<p id="commit" class="anniu"><a href="javascript:void(0);">提交评价</a></p>');
      $("#main").html(html.join(''));
      //上传绑定
      uploadControl = new uploadPage();      
      uploadControl.bindUploadShows();
    },
    paintStar:function(count){
      var strString = [];
      for (var i=0; i<5; i++) {
        if(i<count){
          strString.push('<i class="iconfont icon-xingxing-1"></i>');
        }else{
          strString.push('<i class="iconfont icon-xingxing-1 huizi"></i>');
        }
      }
      return strString.join('');
    },
    bindEvent:function(){
      //点击后退
      $("#back").on("click", function(){
        console.log("back");
        back();        
      });  
      
      //点击星币
      $("#main").on("click", ".icon-xingxing-1", function(e){
        //未选中
        console.log("1");
        if( $(this).hasClass('huizi') ){
          $(this).removeClass('huizi').prevAll().removeClass('huizi');
        }
        //已选中
        else{
          $(this).addClass('huizi').nextAll().addClass('huizi');
        }
        
      });
      
      //提交
      $("#main").on("click", "#commit", function(){
      	var isempty = false;
        var requestData = {
          order_id        : id,
          grade_package   : $("span[name='grade_package']").find("i:not(.huizi)").length,
          grade_deliveriy : $("span[name='grade_deliveriy']").find("i:not(.huizi)").length,
          grade_service   : $("span[name='grade_service']").find("i:not(.huizi)").length,
          member_id       : user.id,
          ip              : $.getIPAddress(),
          //create_time     : "",
          items           : []
        };
        $.each($("div[name='goodsItem']"), function(i, item) {
        	if("" == $(item).find("textarea[name='content']").val()){
        		isempty = true;
        	}
        	var goodsItem = {
        	  grade:$(item).find("span[name='grade']>i:not(.huizi)").length,
        	  content:$(item).find("textarea[name='content']").val(),
        	  tag:"",
        	  sku_id:$(item).attr('tip'),
        	  picture:[],
        	};
        	$.each($(item).find("#showAlbums img[name='addImage']"), function(i, image) {
            var src = $(this).attr('src');
            var index = src.indexOf(',');
            var newStrings = src.substr(index+1);
            goodsItem.picture.push(newStrings);        		
        	});
        	requestData.items.push(goodsItem);
        });
				if(isempty){
					$.toast('请填写评论内容');
					return;
				}
        $.ajax({
        	type:"POST",
        	url: CONFIG_MANAGER.RequstUrl + "v1/orders/eval/img",
        	async:true,
        	data: JSON.stringify( requestData ),
        	//headers:{ token:user.token },
        	contentType: 'application/json',
        	beforeSend:function(){ $.showWaiting(); }
        }).done(function(data){
         	var redirectUrl = 'orders.html?type=finish';
         	redirect(redirectUrl);
        }).always(function(data){
          $.closeWaiting();
        }).fail(function(data){ 
          $.toast("请求失败");
        });

      });
    }
    
  },
  
  thisPage = new orderAppraisePage();
}
